package com.biggabrotha {
	import flash.events.Event;
	import flash.events.StatusEvent;
	import flash.media.Camera;
	import flash.media.Video;
	import flash.system.Security;
	import flash.system.SecurityPanel;

	import mx.containers.Canvas;
	import mx.core.BitmapAsset;

	public class CamPictureProvider extends Canvas implements PictureProvider {

		[Embed(source="../assets/LM_eye_icon.png")]
		private static const nocamImageClass:Class;
		private static const nocamBitmapAsset:BitmapAsset=BitmapAsset(new nocamImageClass());
		[Embed(source="../assets/1984bb.jpg")]
		private static const angryImageClass:Class;
		private static const angryBitmapAsset:BitmapAsset=BitmapAsset(new angryImageClass());
		private var cams:Array;
		private var cam:Camera;
		//should be VideoDisplay?!
		private var vid:Video=new Video();
		private var _isWorking:Boolean;

		public function CamPictureProvider() {
			super();
			this.addEventListener(Event.ADDED_TO_STAGE, addedToStage)
			this.addEventListener(Event.REMOVED_FROM_STAGE, removedFromStage)
			this.rawChildren.addChild(nocamBitmapAsset)
			this.width=nocamBitmapAsset.width
			this.height=nocamBitmapAsset.height
			cams=Camera.names
			trace(cams)
		}

		function camStatusHandler(e:StatusEvent) {
			if (e.code == "Camera.Unmuted") {
				hurray()
			} else {
				showNoCam()
//				Alert.show("Camera access not allowed?!", "Big Brother is angry!")
			}
		}

		private function removedFromStage(e:Event) {
			if (vid)
				if (this.rawChildren.contains(vid))
					this.rawChildren.removeChild(vid)
			if (cam.hasEventListener(StatusEvent.STATUS)) {
				cam.removeEventListener(StatusEvent.STATUS, camStatusHandler)
			}
			cam=null;
			vid=null;
		}

		function showNoCam() {
			if (vid)
				if (this.rawChildren.contains(vid))
					this.rawChildren.removeChild(vid)
			this.rawChildren.addChild(angryBitmapAsset)
			this.width=angryBitmapAsset.width
			this.height=angryBitmapAsset.height
			this.toolTip="Camera access not allowed?!\nBig Brother is angry!"
		}

		function showCam() {
			if (this.contains(nocamBitmapAsset))
				this.removeChild(nocamBitmapAsset)
			if (this.contains(angryBitmapAsset))
				this.removeChild(angryBitmapAsset)
			this.rawChildren.addChild(vid)
			this.width=vid.width
			this.height=vid.height
		}

		private function addedToStage(e:Event) {
			//for now, get the first one
			//ugly expect problems when binding
			cam=Camera.getCamera();
			if (!cam) {
				_isWorking=false
			} else {
				if (cam.muted) {
					cam.addEventListener(StatusEvent.STATUS, camStatusHandler)
					Security.showSettings(SecurityPanel.PRIVACY);
				} else {
					hurray()
				}
			}
//			Security.showSettings(SecurityPanel.CAMERA)
		}

		function hurray() {
			_isWorking=true
			vid=new Video()
//        cam.setMode(320, 240, 10)
			//here comes the security dialog:
			vid.attachCamera(cam)
			showCam()
		}

		public function get isWorking():Boolean {
			return _isWorking
		}
	}
}